import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/Helper.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/fileHelper.dart';
import '../../helper/uidata.dart';
import '../../helper/upload_helper.dart';
import '../../widgets/signatureCard.dart';
import 'package:signature/signature.dart';
import '../../helper/global_config.dart';

import '../../model/skddl.dart';
import '../../widgets/IconWidget.dart';

class SKDDLInfoPage extends StatefulWidget {
  @override
  SKDDLInfoPage(this.s1);
  final SKDDL s1;

  @override
  createState() => new SKDDLInfoPageState();
}

class SKDDLInfoPageState extends State<SKDDLInfoPage> {
  late SKDDL s1;

  @override
  void initState() {
    super.initState();
    s1 = widget.s1;
  }

  upload(File file1) async {
    await Helper.GetstoragePermission();
    var imgId = await FileHelper.UpLoad(file1);

    if (s1.BMUserID == UIData.CurrentUser!.Guid) {
      s1.BMIfOK = true;
      s1.BMTM = Helper.DateTimeNowString();
      s1.BMQM = imgId;
      s1.BMHQ = "同意";
    }

    if (s1.DCUserID == UIData.CurrentUser!.Guid) {
      s1.DCIfOK = true;
      s1.DCTM = Helper.DateTimeNowString();
      s1.DCQM = imgId;
      s1.DCHQ = "同意";
    }

    if (s1.GCBUserID == UIData.CurrentUser!.Guid) {
      s1.GCBIfOK = true;
      s1.GCBTM = Helper.DateTimeNowString();
      s1.GCBQM = imgId;
      s1.GCBHQ = "同意";
    }

    if (s1.LDUserID == UIData.CurrentUser!.Guid) {
      s1.LDIfOK = true;
      s1.LDTM = Helper.DateTimeNowString();
      s1.LDQM = imgId;
      s1.LDHQ = "同意";
    }

    SKDDL.Update(s1);
    var s2 = await SKDDL.Get("ID=@ID", {"ID": s1.ID});
    if (s2 != null) {
      setState(() {
        s1 = s2;
      });
    }
  }

  Widget getCLWidget(bool ifOk, String content, String imgId) {
    if (!ifOk)
      return ListTile(
          title: Text(
        "未处理",
        style: TextStyle(color: Colors.black54, height: 2.0),
      ));
    return ListTile(
      title: Text(
        content,
        style: TextStyle(color: Colors.black54, height: 2.0),
      ),
      trailing: Image.network(Helper.GetImgUrl(imgId)),
    );
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];
    list1.add(ListTile(
      title: Text("带班领导"),
      trailing: Text(CacheHelper.GetUser(s1.BMUserID).Name),
    ));
    list1.add(getCLWidget(s1.BMIfOK, s1.BMHQ, s1.BMQM));
    if (!s1.BMIfOK) return list1;
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(ListTile(
      title: Text("电厂负责人"),
      trailing: Text(CacheHelper.GetUser(s1.DCUserID).Name),
    ));
    list1.add(getCLWidget(s1.DCIfOK, s1.DCHQ, s1.DCQM));
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(ListTile(
      title: Text("工程部负责人"),
      trailing: Text(CacheHelper.GetUser(s1.GCBUserID).Name),
    ));
    list1.add(getCLWidget(s1.GCBIfOK, s1.GCBHQ, s1.GCBQM));
    list1.add(Divider(
      height: 0.0,
    ));
    if (!s1.GCBIfOK || !s1.DCIfOK) return list1;

    list1.add(ListTile(
      title: Text("公司分管领导"),
      trailing: Text(CacheHelper.GetUser(s1.LDUserID).Name),
    ));
    list1.add(getCLWidget(s1.LDIfOK, s1.LDHQ, s1.LDQM));
    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCardPage(upload, "签字版");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    if (s1 == null) return new Container();

    // TODO: implement build
    return Scaffold(
        appBar: AppBar(title: Text('调度令详情')),
        //bottomSheet:bottomPanel2() ,
        floatingActionButton: FloatingActionButton(
            child: Icon(FontAwesomeIcons.penAlt),
            onPressed: () {
              showPanel(context);
            }),
        body: SingleChildScrollView(
          child: new Container(
              margin: EdgeInsets.only(top: 8.0),
              child: new Column(children: <Widget>[
                Card(
                  child: ListTile(
                    leading: IconWidget.RiverImage(),
                    title: Text("水库调度令"),
                  ),
                  color: GlobalConfig.cardBackgroundColor,
                  margin: const EdgeInsets.all(6.0),
                ),
                Card(
                  child: Column(
                    children: <Widget>[
                      ListTile(
                        title: Text("调度令简述"),
                      ),
                      Divider(
                        height: 0.0,
                      ),
                      ListTile(
                        title: Text(
                          s1.Content,
                          style: TextStyle(color: Colors.black54, height: 2.0),
                        ),
                      ),
                    ],
                  ),
                  color: GlobalConfig.cardBackgroundColor,
                  margin: const EdgeInsets.all(6.0),
                ),
                Card(
                  child: Column(
                    children: getWidgetList(),
                  ),
                  color: GlobalConfig.cardBackgroundColor,
                  margin: const EdgeInsets.all(6.0),
                ),
              ])),
        ));
  }
}
